/**
 * TorrentJEDI
 * 		Torrent Java Episode Download Interface
 */
package torrentjedi.ui;

import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.WindowConstants;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import torrentjedi.core.Configuration;
import torrentjedi.core.TorrentSource;
import torrentjedi.data.SourceConfigurationWriter;
import torrentjedi.util.JEDIValidator;

/**
 * @author Alper Kokmen
 *
 */
public class NewSourceDialog extends JDialog implements ActionListener {
	private Configuration configuration;
	
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jNameLabel = null;
	private JLabel jURLLabel = null;
	private JLabel jIconLabel = null;
	private JLabel jFavoriteLabel = null;
	private JLabel jUpdateIntervalLabel = null;
	private JLabel jAutoLoadLabel = null;
	private JTextField jNameTextField = null;
	private JTextField jURLTextField = null;
	private JTextField jUpdateIntervalTextField = null;
	private JCheckBox jAutoLoadCheckBox = null;
	private JCheckBox jFavoriteCheckBox = null;
	private JButton jSaveButton = null;
	private JButton jCancelButton = null;
	private JLabel jMinutesLabel = null;
	/**
	 * @param owner
	 */
	public NewSourceDialog(Frame owner, Configuration configuration) {
		super(owner);
		this.configuration = configuration;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(425, 250);
		this.setLocationRelativeTo(getOwner());
		this.setTitle("Add New Torrent Source");
		this.setResizable(false);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jMinutesLabel = new JLabel();
			jMinutesLabel.setText(" minutes");
			jMinutesLabel.setSize(new Dimension(75, 20));
			jMinutesLabel.setLocation(new Point(150, 75));
			jAutoLoadLabel = new JLabel();
			jAutoLoadLabel.setText("Auto Load");
			jAutoLoadLabel.setSize(new Dimension(90, 20));
			jAutoLoadLabel.setLocation(new Point(135, 135));
			jUpdateIntervalLabel = new JLabel();
			jUpdateIntervalLabel.setText("Favorite");
			jUpdateIntervalLabel.setSize(new Dimension(90, 20));
			jUpdateIntervalLabel.setLocation(new Point(15, 135));
			jFavoriteLabel = new JLabel();
			jFavoriteLabel.setText("Icon");
			jFavoriteLabel.setSize(new Dimension(90, 20));
			jFavoriteLabel.setLocation(new Point(15, 105));
			jIconLabel = new JLabel();
			jIconLabel.setText("Update Interval");
			jIconLabel.setSize(new Dimension(90, 20));
			jIconLabel.setLocation(new Point(15, 75));
			jURLLabel = new JLabel();
			jURLLabel.setText("URL");
			jURLLabel.setSize(new Dimension(90, 20));
			jURLLabel.setLocation(new Point(15, 45));
			jNameLabel = new JLabel();
			jNameLabel.setLocation(new Point(15, 15));
			jNameLabel.setSize(new Dimension(90, 20));
			jNameLabel.setText("Name");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jNameLabel, null);
			jContentPane.add(jURLLabel, null);
			jContentPane.add(jIconLabel, null);
			jContentPane.add(jFavoriteLabel, null);
			jContentPane.add(jUpdateIntervalLabel, null);
			jContentPane.add(jAutoLoadLabel, null);
			jContentPane.add(getJNameTextField(), null);
			jContentPane.add(getJURLTextField(), null);
			jContentPane.add(getJUpdateIntervalTextField(), null);
			jContentPane.add(getJAutoLoadCheckBox(), null);
			jContentPane.add(getJFavoriteCheckBox(), null);
			jContentPane.add(getJSaveButton(), null);
			jContentPane.add(getJCancelButton(), null);
			jContentPane.add(jMinutesLabel, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jNameTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJNameTextField() {
		if (jNameTextField == null) {
			jNameTextField = new JTextField();
			jNameTextField.setLocation(new Point(105, 15));
			jNameTextField.setSize(new Dimension(135, 20));
			jNameTextField.setToolTipText("5 to 50 characters long");
		}
		return jNameTextField;
	}

	/**
	 * This method initializes jURLTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJURLTextField() {
		if (jURLTextField == null) {
			jURLTextField = new JTextField();
			jURLTextField.setLocation(new Point(105, 45));
			jURLTextField.setSize(new Dimension(286, 20));
			jURLTextField.setToolTipText("a valid RSS url is required");
		}
		return jURLTextField;
	}

	/**
	 * This method initializes jUpdateIntervalTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJUpdateIntervalTextField() {
		if (jUpdateIntervalTextField == null) {
			jUpdateIntervalTextField = new JTextField();
			jUpdateIntervalTextField.setLocation(new Point(105, 75));
			jUpdateIntervalTextField.setSize(new Dimension(46, 20));
			jUpdateIntervalTextField.setToolTipText("a value between 1 and 300");
		}
		return jUpdateIntervalTextField;
	}

	/**
	 * This method initializes jAutoLoadCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJAutoLoadCheckBox() {
		if (jAutoLoadCheckBox == null) {
			jAutoLoadCheckBox = new JCheckBox();
			jAutoLoadCheckBox.setLocation(new Point(225, 135));
			jAutoLoadCheckBox.setSize(new Dimension(20, 20));
		}
		return jAutoLoadCheckBox;
	}

	/**
	 * This method initializes jFavoriteCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJFavoriteCheckBox() {
		if (jFavoriteCheckBox == null) {
			jFavoriteCheckBox = new JCheckBox();
			jFavoriteCheckBox.setLocation(new Point(105, 135));
			jFavoriteCheckBox.setSize(new Dimension(20, 20));
		}
		return jFavoriteCheckBox;
	}

	/**
	 * This method initializes jSaveButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJSaveButton() {
		if (jSaveButton == null) {
			jSaveButton = new JButton();
			jSaveButton.setText("Save");
			jSaveButton.setSize(new Dimension(75, 25));
			jSaveButton.setLocation(new Point(15, 180));
			jSaveButton.addActionListener(this);
		}
		return jSaveButton;
	}

	/**
	 * This method initializes jCancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJCancelButton() {
		if (jCancelButton == null) {
			jCancelButton = new JButton();
			jCancelButton.setText("Cancel");
			jCancelButton.setSize(new Dimension(75, 25));
			jCancelButton.setLocation(new Point(105, 180));
			jCancelButton.addActionListener(this);
		}
		return jCancelButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("DEBUG@TorrentJEDI:NewSourceDialog actionPerformed");
		
		JButton source = (JButton) e.getSource();
		
		if (source.getText().equals("Cancel")) {
			this.dispose();
		} else if (source.getText().equals("Save")) {
			boolean valid;
			valid = JEDIValidator.validateText(getJNameTextField().getText(), 5, 50) ||
					JEDIValidator.validateURL(getJURLTextField().getText()) ||
					JEDIValidator.validateInteger(getJUpdateIntervalTextField().getText(), 1, 300);
			
			if (valid) {
				TorrentSource newSource = new TorrentSource();
				
				newSource.setName(getJNameTextField().getText());
				newSource.setUrl(getJURLTextField().getText());
				newSource.setUpdateInterval(Integer.parseInt(getJUpdateIntervalTextField().getText()));
				newSource.setIcon("TODO");
				newSource.setAutoLoad(getJAutoLoadCheckBox().isSelected());
				newSource.setFavorite(getJFavoriteCheckBox().isSelected());
			
				SourceConfigurationWriter sourceConfigurationWriter = new SourceConfigurationWriter(getConfiguration());
				sourceConfigurationWriter.writeSourceConfiguration(newSource);
				
				this.dispose();
			}
		}
		
	}

	/**
	 * @return the configuration
	 */
	public Configuration getConfiguration() {
		return configuration;
	}

	/**
	 * @param configuration the configuration to set
	 */
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

}